<template>
  <div>
    <!-- --------------------------------搜索框----------------------------- -->
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input
            v-model="query.userName"
            placeholder="用户名"
            :clearable="true"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="query.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始注册日期"
            end-placeholder="结束注册日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="loadUsers"
            >搜索</el-button
          >
          <el-button type="success" @click="dialog.addShow = true"
            >新建</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- -----------------------------分页显示数据--------------------------------------- -->
    <el-table :data="userList" border style="width: 100%">
      <el-table-column type="index" width="50" label="序号"> </el-table-column>

      <el-table-column prop="userName" label="账号" width="150">
      </el-table-column>
      <el-table-column prop="nickName" label="姓名" width="150">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="150">
      </el-table-column>
      <el-table-column prop="tel" label="电话" width="150"> </el-table-column>
      <el-table-column prop="deptName" label="部门" width="150">
      </el-table-column>
      <el-table-column label="头像" show-overflow-tooltip width="150">
        <template slot-scope="scope">
          <img
            :src="scope.row.headImg"
            alt=""
            style="width: 60px; height: 60px"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            title="这是一段内容确定删除吗？"
            @confirm="handleDelete(scope.row.id)"
          >
            <el-button
              type="danger"
              slot="reference"
              size="mini"
              icon="el-icon-delete"
              >删除</el-button
            >
          </el-popconfirm>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-user-solid"
            @click="queryRole(scope.row.id)"
            >分配角色</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.currentPage"
      :page-sizes="pageInfo.pageSizes"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
    >
    </el-pagination>
    <!-- ------------------------------------添加操作------------------------------------ -->
    <el-dialog title="添加用户" :visible.sync="dialog.addShow" width="30%">
      <el-form ref="addForm" :model="addUser" :rules="rules">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="userName"
        >
          <el-input v-model="addUser.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            v-model="addUser.password"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item
          label="昵称"
          :label-width="formLabelWidth"
          prop="nickName"
        >
          <el-input v-model="addUser.nickName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth" prop="tel">
          <el-input v-model="addUser.tel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth" prop="deptId">
          <el-select v-model="addUser.deptId" placeholder="请选择部门">
            <el-option
              v-for="(dept, index) in deptList"
              :key="index"
              :label="dept.deptName"
              :value="dept.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.addShow = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- ------------------------------------修改操作------------------------------------ -->
    <el-dialog title="修改用户" :visible.sync="dialog.updateShow" width="30%">
      <el-form :model="updateUser">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="updateUser.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input
            v-model="updateUser.password"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="昵称" :label-width="formLabelWidth">
          <el-input v-model="updateUser.nickName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="updateUser.tel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth">
          <el-select v-model="updateUser.deptId" placeholder="请选择部门">
            <el-option
              v-for="(dept, index) in deptList"
              :key="index"
              :label="dept.deptName"
              :value="dept.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.updateShow = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- ------------------------------------给用户分配角色操作------------------------------------ -->
    <el-dialog title="用户授权" :visible.sync="dialog.roleShow" width="30%">
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="(role, index) in roleList" :key="index" :label="role.id">{{role.roleName}}</el-checkbox>        
      </el-checkbox-group>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.roleShow = false">取 消</el-button>
        <el-button type="primary" @click="assignRole()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      uid: 0,
      roleList:[],
      checkList: [],
      deptList: [],
      formLabelWidth: "100px",
      addUser: {},
      updateUser: {},
      rules: {
        userName: [
          { required: true, message: "请输入正确的用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        region: [
          { required: true, message: "请选择活动区域", trigger: "change" },
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        date2: [
          {
            type: "date",
            required: true,
            message: "请选择时间",
            trigger: "change",
          },
        ],
        type: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个活动性质",
            trigger: "change",
          },
        ],
        resource: [
          { required: true, message: "请选择活动资源", trigger: "change" },
        ],
        desc: [{ required: true, message: "请填写活动形式", trigger: "blur" }],
      },
      dialog: {
        addShow: false,
        updateShow: false,
        roleShow: false,
      },
      query: {
        userName: "",
        dateRange: [],
        currentPage: 1, //当前页
        pageSize: 5, //每页显示的条数
      },
      pageInfo: {
        total: 0, //总条数
        pageSizes: [5, 10, 20, 50],
      },
      userList: [],
    };
  },
  methods: {
    queryRole(uid) {
      this.dialog.roleShow = true;
      this.uid=uid
      this.$axios.get("/roles/"+uid)
      .then(res=>{
        this.roleList = res.data.data.roleList;//得到系统所有的角色
        this.checkList = res.data.data.checkList;//得到当前用户拥有的角色        
      })
    },
    assignRole() {
      this.$axios.put("/roles",{uid:this.uid,rids:this.checkList})
      .then(res=>{
        if(res.data.code==200){
          this.$message.success(res.data.message);
          this.dialog.roleShow = false;
        }else{
          this.$message.error(res.data.message);
        }
      })      
    },
    handleEdit(id) {
      this.$axios.get("/users/" + id).then((res) => {
        this.updateUser = res.data.data.user;
        this.dialog.updateShow = true; //显示模态框
      });
    },
    update() {
      this.$axios.put("/users/", this.updateUser).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.dialog.updateShow = false; //关于模态框
          this.loadUsers(); //重新加载数据
        } 
        /*
        else {
          this.$message.error(res.data.message);
        }
        */
      });
    },
    handleDelete(id) {
      this.$axios.delete("/users/" + id).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.loadUsers(); //重新加载数据
        } 
        /*
        else {
          this.$message.error(res.data.message);
        }
        */
      });
    },
    loadDept() {
      this.$axios.get("/depts").then((res) => {
        this.deptList = res.data.data.list;
      });
    },

    add() {
      this.$axios.post("/users/", this.addUser).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.dialog.addShow = false; //关于模态框
          this.$refs.addForm.resetFields(); //清空表单数据
          //this.addUser={};
          this.loadUsers(); //重新加载数据
        } /*
        else {
          this.$message.error(res.data.message);
        }
         */
      });
    },
    handleSizeChange(val) {
      this.query.pageSize = val;
      this.loadUsers();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.loadUsers();
    },
    loadUsers() {
      this.$axios.post("/users/queryAll", this.query).then((res) => {
        this.userList = res.data.data.list; //返回当前页数据
        this.pageInfo.total = res.data.data.total; //返回一共有多少条数据
      });
    },
  },
  mounted() {
    this.loadUsers();
    this.loadDept();
  },
};
</script>